package mrusanov.staff

import java.util.Date
import mrusanov.team.Club
import mrusanov.money.ContractTerminationMoneyChangeReason
import mrusanov.player.Player

trait Contracted {
  var contract:Option[Contract] = None

  def updateContract(club:Club, now:Date) {
     if (contract.exists(_.isOver(now))) {
       contract = None
       removeContractedFromClub(club)
     }
  }

  def moneyForContractTermination(now:Date) = contract.map(_.contractSumLeft(now) / 2).getOrElse(0)

  def terminateContract(now:Date, club:Club) {
    require(contract.isDefined, "Contract is not defined")

    val reason = ContractTerminationMoneyChangeReason(moneyForContractTermination(now))
    contract = None
    club.clubBudget.acceptMoneyChangeReason(reason)
    removeContractedFromClub(club)
  }

  private def removeContractedFromClub(club:Club) {
    this match {
      case emp:ClubEmployee => club.clubStaff.removeEmployee(emp)
      case player:Player => club.removePlayer(player)
      case _ => throw new IllegalArgumentException("This is nor employee neither player!")
    }
  }

}
